//app.js

App({
  onLaunch: function () {
    
    if (!wx.cloud) {
      console.error('请使用 2.2.3 或以上的基础库以使用云能力')
    } else {
      wx.cloud.init({
        env: "cards-2c53b0",
        traceUser: true,
      });
    }
    
    var that=this;
    this.getOpenId(function(openid){
      that.globalData={
        openid: openid,
        desks: []
      };
      const Desk = require("./lib/cardDesk.js");
      Desk.getDesk(openid, function (list) {
        that.globalData.desks=list;
      })
    })
  },
  getOpenId(callback) {
    let idCookie = wx.getStorageSync("openid");
    if (idCookie) {
      console.log("使用了openid缓存", idCookie);
      callback(idCookie);
    }
    else {
      let that = this;
      let date = new Date();
      let time = date.toLocaleString();
      wx.cloud.callFunction({
        name: "getId",
        data: {
          date: time
        },
        success(res) {
          wx.setStorage({
            key: 'openid',
            data: res.result.openid
          });
          callback(res.result.openid);
        },
        fail(res) {
          callback(0);
        }
      })
    }
  }
})
